package widget;

import sun.awt.image.ImageWatched;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by LLAP on 2017/12/13.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class QueryTable extends JPanel {

    private LinkedList<TRow> rowList;

    public QueryTable(boolean border) {
        super();
        rowList = new LinkedList<>();
        if (border) this.setBorder(new LineBorder(new Color(180, 180, 180),1, false));
    }

    public QueryTable() {
        this(false);
    }

    public JPanel pack() {
        super.setLayout(new GridLayout(rowList.size(), 1, 10, 10));
        for (TRow i : rowList) {
            this.add(i);
        }
//        this.updateUI();
        return this;
    }

    public TRow addRow() {
        TRow ret = new TRow();
        rowList.add(ret);
        return ret;
    }

    public TRow addRow(TRow r) {
        rowList.add(r);
        return r;
    }

    public TRow getRow(int index) {
        return rowList.get(index);
    }

    public void clear() {
        for (TRow i : rowList) {
            i.clear();
        }
        this.updateUI();
    }

    public HashMap<String, Object> getData() throws IllegalArgumentException {
        try {
            return new HashMap<String, Object>() {{
                for (TRow r : rowList) {
                    putAll(r.getData());
                }
            }};
        }
        catch (IllegalArgumentException e) {
            throw e;
        }
    }
}
